<?
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

require_once 'sites/default/settings.php';
ini_set( 'magic_quotes_gpc', 0 );
#require("includes/phpunit.php");
require_once 'includes/class.phpmailer.php';
require_once 'includes/karabas.class.php';
require_once 'includes/flashbox.class.php';
require_once 'includes/inetbilet.class.php';
require_once 'includes/infopark.class.php';
$infopark=new Infopark;
if($_GET['nid'])
{
    
#$node=node_load(array('nid'=>$_GET['nid']));
	$query = db_select('node', 'n');

    $query->leftJoin('field_data_field_schedule_event', 's_event', 'n.nid = s_event.entity_id');
    $query->leftJoin('node', 'event_node', 'event_node.nid = s_event.field_schedule_event_nid');
   
	$query->leftJoin('field_data_field_schedule_start', 's_start', 's_event.entity_id = s_start.entity_id');
	$query->leftJoin('field_data_field_schedule_end', 's_end', 's_event.entity_id = s_end.entity_id');
    $query->leftJoin('field_data_field_schedule_place', 's_place', 'n.nid = s_place.entity_id');
	$query->leftJoin('field_data_field_schedule_price', 's_price', 's_event.entity_id = s_price.entity_id');
	$query->leftJoin('node', 'p_node', 'p_node.nid = s_place.field_schedule_place_nid');
    $query->leftJoin('node', 'place_node', 's_place.field_schedule_place_nid = place_node.nid');
	$query->leftJoin('field_data_field_place_city', 'place_city', 'place_city.entity_id = s_place.field_schedule_place_nid');
	$query->leftJoin('field_data_field_place_city', 'place_city', 'place_city.entity_id = s_place.field_schedule_place_nid');
	$query->leftJoin('taxonomy_term_data', 'ttd', 'ttd.tid = place_city.field_place_city_tid');
	$query->leftJoin('field_data_field_inet_bilet', 'ib', 'ib.entity_id = s_event.entity_id');
	$query->leftJoin('field_data_field_karabas', 'kar', 'kar.entity_id = s_event.entity_id');
        $query->leftJoin('field_data_field_flashbox', 'flash', 'flash.entity_id = s_event.entity_id');
	#$query->leftJoin('field_data_field_url', 'url', 'url.entity_id = s_event.entity_id');
	#$query->leftJoin('field_data_field_description', 'descr2', 'descr2.entity_id = s_event.entity_id');
	#$query=db_select('field_data_field_schedule_place', 's_place');
$query->condition('s_event.entity_id',$_GET['nid']);
#$query->condition('s_start.field_schedule_start_value',date('Y-m-d H:i:s',$_GET['date']));
$query->fields('s_place',array('field_schedule_place_nid'))
->fields('s_start',array('field_schedule_start_value'))
->fields('s_end',array('field_schedule_end_value'))
->fields('s_price',array('field_schedule_price_value'))
->fields('p_node',array('title'))
->fields('event_node',array('title','nid'))
->fields('place_city',array('field_place_city_tid'))
->fields('ttd',array('name'))
->fields('ib',array('field_inet_bilet_value'))
->fields('kar',array('field_karabas_value'))
->fields('flash',array('field_flashbox_value'))
#->fields('url',array('field_url_value'))
#->fields('descr2',array('field_description_value'))
;
$query->groupBy('s_start.field_schedule_start_value');
 $event = $query->execute()->fetchObject();
 }

if(count($_POST))
{


$subject = "Заказ билета на ".$_POST['date'];

$body = '
<html>
    <head>
        <title>"Заказ билета на '.$_POST['date'].'</title>
    </head>
    <body>
		<table>
        <tr><td>Имя:</td><td>'.$_POST['name'].'</td></tr>
		<tr><td>Телефон:</td><td>'.$_POST['phone'].'</td></tr>
		<tr><td>Дата:</td><td>'.$_POST['date'].'</td></tr>
		<tr><td>Событие:</td><td>'.l($event->event_node_title,'node/'.$event->nid,array('absolute'=>true,'query'=>array('evt'=>$_GET['nid']))).' в '.l($event->title,'node/'.$event->field_schedule_place_nid,array('absolute'=>true)).'</td></tr>
		</table>
    </body>
</html>';
#

#die($body);
#$headers  = "Content-type: text/html; charset=windows-1251 \r\n";
#$headers .= "From: Infopark Orders <info@infopark.com.ua>\r\n";
#$headers .= "Bcc: birthday-archive@example.com\r\n";

#$rez=mail($conf['order_tickets_email'], $subject, $message, $headers); 


$mail             = new PHPMailer(); // defaults to using php "mail()"

#$body             = $mail->getFile('contents.html');
#$body             = eregi_replace("[\]",'',$body);

$mail->From       = "webmaster@infopasrk.ua";
$mail->FromName   = "Infopark Orders";

$mail->Subject    = $subject;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAddress($conf['order_tickets_email'], "");
#die($conf['order_tickets_email']);
 $mail->AddAddress("shaman33@gmail.com", "");
 $mail->AddAddress("shvorinjm@gmail.com", "");

#$mail->AddAttachment("images/phpmailer.gif");             // attachment
$rez=$mail->Send();
?>

<html>
<body>
<?
if($rez)
{
?>
<h1>Спасибо.</h1>
<h2>Ваше обращение принято, в скором времени мы свяжемся с вами.</h2>
<?}else{?>
<h2>Извините, произошла непредвиденная ошибка</h2>
<2>Попробуйте пожалуйста заказать позже.</2>
<?}?>
<input class="cal_btn_clear" type="button" value="закрыть" onclick="modal.close();">
</body>
</html> 
<?
}else{
#print $_GET['date']."<hr>";
#d=strtotime($_GET['date']);
#print_r($_GET);

#pre($node);
?>
<html>
<body>
<h1><?=$event->event_node_title;?></h1><h2><?=$infopark->draw_dm($event->field_schedule_start_value);?> <?=date('H:i',strtotime($event->field_schedule_start_value));?></h2>
<?


/*    <h1>Заказ билетов на <?=l($node->title,'node/'.$node->nid);?> в <?=l($event->title,'node/'.$event->field_schedule_place_nid);?>, <b><?=$event->name;?></b>
	</h1>

	<div>
	$imgurl=(!empty($event->field_url_value))?$event->field_url_value:image_style_url('w150',$node->field_event_thumbnail['und'][0]['uri']);
	?><img src="<?=$imgurl;?>" style="float:left;margin:0 5px 5px 0;">
	<?
#(!empty($event->field_description_value))?$event->field_description_value:$node->field_event_description['und'][0]['value'];?>
	</div>
	
	<div><a href="/events?day=<?=date('dmY',strtotime($event->field_schedule_start_value));?>"><strong><?=date('j',strtotime($event->field_schedule_start_value));?></strong> <?=$conf['month_namez'][date('n',strtotime($event->field_schedule_start_value))-1];?> <?=date('Y',strtotime($event->field_schedule_start_value));?>/<span><?=$conf['week_days_short'][date('w',strtotime($event->field_schedule_start_value))];?></span></a></div>
	

		<div class="od_tm">Цена: <?=$event->field_schedule_price_value;?></div>
	<div class="od_tm">Начало в <?=date('H:i',strtotime($event->field_schedule_start_value));?></div>
	<?	
										if($event->field_schedule_end_value)
										{
										?><div class="od_tm">Окончание в <?=date('H:i',strtotime($event->field_schedule_end_value));  
										if(date('d',strtotime($event->field_schedule_start_value))!=date('d',strtotime($event->field_schedule_end_value)))
											{
											echo date('j ',strtotime($event->field_schedule_end_value)).$conf['month_namez'][date('n',strtotime($event->field_schedule_end_value))-1];
											}
										?></div><?
										}

*/
	#switch($event->field_provider_value)
		#{
#$order_provider.='';
#'<a href="">Купить Билет</a>'

if(!empty($event->field_karabas_value))
{
  
    $karabas=new Karabas;
    $result=$karabas->get_event_stat($event->field_karabas_value);
    echo $karabas->draw_event_stat($event,$result);
   
        
    #$order_provider.="<input type='button' class='provider_btn' value='Купить через Карабас' onclick='_gaq.push([\"_trackEvent\", \"buy_ticket\", \"way_of_buying\", \"karabas\"]);window.open(\"http://infopark.frontmanager.com.ua/PublicAPI/Tickets.aspx?__pid=aLxxqPoctjko0A2WV2GnRQ&__uid=oDFXEGRXKVkKCGH1xRvG7g&event=".$event->field_karabas_value."\");'>";
}
if(!empty($event->field_inet_bilet_value))
{
    $inetbilet=new Inetbilet;
    $result_ib=$inetbilet->show_event_info($event->field_inet_bilet_value);
    #$result_ib=$inetbilet->show_event_info(2769);
    print $result_ib;
   # $order_provider.="<input type='button' class='provider_btn' value='Купить через Интернет-Билет' onclick='_gaq.push([\"_trackEvent\", \"buy_ticket\", \"way_of_buying\",\"internet_bilet\"]);window.open(\"http://internet-bilet.ua/events/item.html?event_id=".$event->field_inet_bilet_value."&partner=infopark\")'>";
}
if(!empty($event->field_flashbox_value))
{
    $flashbox=new Flashbox;
    //$result=$flashbox->get_event_stat($event->field_flashbox_value);
    echo $flashbox->show_event_info($event->field_flashbox_value);
    
   # $order_provider.="<input type='button' class='provider_btn' value='Купить через Интернет-Билет' onclick='_gaq.push([\"_trackEvent\", \"buy_ticket\", \"way_of_buying\",\"internet_bilet\"]);window.open(\"http://internet-bilet.ua/events/item.html?event_id=".$event->field_inet_bilet_value."&partner=infopark\")'>";
}
//echo "<h4>Ваш заказ будет обработан одной и з выбранных Вами систем-партнеров:</h4>";
#echo $order_provider;
if(strtotime($event->field_schedule_start_value)>time()):

	?>
<a class="order_tickets_but order_btn operatorbtn"  onclick="jQuery('#leaf_phone').show();jQuery('.provider_btn').hide();jQuery('.order_btn').hide();">Или Заказать у оператора</a>

<div style="display:none" id="leaf_phone">										
<h2>Оставьте свой телефон и имя, и мы свяжемся с вами для приёма заказа.</h2>
<form  name="order" class="orderform">

    <div class="formlabel">Имя:</div>
    <input type="text" name="name" class="form-control">
    <div class="formlabel">Телефон:</div>
    <input type="text" class="form-control" name="phone" value="+38">
<input type="hidden" name="date" value="<?=date('d-m-Y H:i',strtotime($event->field_schedule_start_value));?>">
<input type="hidden" class="form-control" name="nid" value="<?=htmlspecialchars($_GET['nid']);?>">
<input type="hidden" class="form-control" name="tdate" value="<?=strtotime($event->field_schedule_start_value);?>">
<a href="javascript://" onclick="_gaq.push(['_trackEvent', 'buy_ticket', 'way_of_buying', 'callback']);submit_order()" class="order_tickets_but">Заказать</a><br>
<a href="javascript://" onclick="jQuery('#leaf_phone').hide();jQuery('.provider_btn').show();jQuery('.order_btn').show();" class="cal_btn_clear">Отменить</a>

<!--input type="button" value="Заказать" onclick="submit_order()" class="butt"-->

</form>
</div>
<?if(empty($event->field_karabas_value)
     &&empty($event->field_inet_bilet_value)
     &&empty($event->field_flashbox_value)   
        ):?>
<script>
jQuery( document ).ready(function() {
jQuery('#leaf_phone').show();jQuery('.provider_btn').hide();jQuery('.order_btn').hide();
});
</script>
<?endif;?>
<?else:?>
<div><b>Событие уже прошло</b></div>
<?endif;?>
</body>
</html> 
<?}?>